package com.region.integral.common_user.obtain.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.region.integral.common_user.obtain.dao.IIntegralJoinActivity;
import com.region.integral.common_user.obtain.vo.IntegralData;
import com.region.integral.common_user.pojo.CUIEntity;
import com.region.integral.common_user.xml.Regulation;
import com.region.integral.common_user.xml.dto.IssueActivity;
import com.region.integral.common_user.xml.dto.JoinActivity;
import com.region.integral.exception.IntegralException;
import com.region.integral.hibernate.BaseHibernate;
import com.region.integral.util.XMLRegulationUtil;
@Component("iJoinActivity")
public class IntegralJoinActivityImpl extends BaseHibernate implements IIntegralJoinActivity {

	@Override
	public void joinActivity(int userid,IntegralData integralData)throws IntegralException {
		CUIEntity cuiEntity = getCUIEntity(userid);
		if (cuiEntity != null) {
			int joinActivityNum = cuiEntity.getJoinActivityNum();
			Regulation regulation = XMLRegulationUtil.unmarshaller();
			List<JoinActivity> joinActivities = regulation.getJoinactivity();
			for (JoinActivity joinActivity : joinActivities) {
				if (joinActivityNum % joinActivity.getRate() == 0) {
					int currIntegral = cuiEntity.getCurrIntegral();
					int maxIntegral = cuiEntity.getMaxIntegral();
					int totalIntegral = currIntegral+ joinActivity.getIntegral();
					cuiEntity.setCurrIntegral(totalIntegral);
					if (maxIntegral < totalIntegral) {
						cuiEntity.setMaxIntegral(totalIntegral);
					}
					cuiEntity.setJoinActivityNum(joinActivityNum+1);
					integralData.setSuccee(true);
					integralData.setObtainIntegral(joinActivity.getIntegral());
					hibernateTemplate.update(cuiEntity);
				}
				
			}
		}
	}

}
